package com.sunday.aomicaichang.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.constValues.ConstValue;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.Village;
import com.sunday.aomicaichang.ui.MainActivity;
import com.sunday.aomicaichang.ui.SelectAreaActivity;
import com.sunday.member.SessionToken;
import com.sunday.member.entity.Member;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/5/31.
 */

public class LoginAreaActivity extends BaseActivity {

    @Bind(R.id.tv_area)
    TextView tvArea;
    @Bind(R.id.btn_register)
    TextView btnRegister;
    @Bind(R.id.ly_register)
    RelativeLayout lyRegister;
    private String code, password, phone;
    private int type;
    private String token;
    private String logo;
    private String nickName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register3);
        ButterKnife.bind(this);
        btnRegister.setText("立即登录");
        code = getIntent().getStringExtra("code");
        password = getIntent().getStringExtra("password");
        phone = getIntent().getStringExtra("phone");
        type=getIntent().getIntExtra("type",0);
        token=getIntent().getStringExtra("token");
        logo=getIntent().getStringExtra("logo");
        nickName=getIntent().getStringExtra("nickName");
    }

    @OnClick({R.id.ly_register, R.id.btn_register})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ly_register:
                intent = new Intent(mContext, SelectAreaActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.btn_register:
                int villageId = SharePerferenceUtils.getIns(mContext).getInt(ConstValue.AREA, 0);
                Call<ResultDO<Member>> call = ApiClient.getApiAdapter().loginWithThirdThree(token,password,nickName,logo,type,code,phone,
                         villageId);
                call.enqueue(new Callback<ResultDO<Member>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                        dissMissDialog();
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        ResultDO<Member> resultDO = response.body();
                        if (resultDO.getCode() == 0) {
                            ToastUtils.showToast(mContext, "登录成功");
                            SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                            BaseApp.getInstance().setMember(resultDO.getResult());
                            SessionToken.accountId= String.valueOf(resultDO.getResult().getId());
                            SessionToken.token=resultDO.getResult().getToken();
                            intent=new Intent(mContext, MainActivity.class);
                            startActivity(intent);
                        } else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                        dissMissDialog();
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Village.ChildrenBeanX.ChildrenBean  childrenBean= (Village.ChildrenBeanX.ChildrenBean ) data.getSerializableExtra("area");
        if(childrenBean.getName()!=null)
        tvArea.setText(childrenBean.getName());
        SharePerferenceUtils.getIns(mContext).putInt(ConstValue.AREA, childrenBean.getId());
        SharePerferenceUtils.getIns(mContext).putString(ConstValue.AREA_NAME, childrenBean.getName());

    }
}
